Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open files with spaces in filename #1231

Merged
merged 1 commit into from
Dec 12, 2021

Conversation

ath3
Copy link
Contributor

@ath3 ath3 commented Dec 3, 2021

No description provided.

Comment on lines 1926 to 2002
if args.is_empty() {
bail!("wrong argument count")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if args.is_empty() {
bail!("wrong argument count")
}
ensure!(!args.is_empty(), "wrong argument count");

@@ -1923,7 +1923,8 @@ mod cmd {
args: &[&str],
_event: PromptEvent,
) -> anyhow::Result<()> {
let path = args.get(0).context("wrong argument count")?;
ensure!(!args.is_empty(), "wrong argument count");
let path = args.join(" ");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we teach the arg parser to deal with quotes instead? I can imagine using :open to open multiple files

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reworked it to support doublequotes for the filenames with space(s), and opening multiple files at once.

@ath3 ath3 force-pushed the open-space-filename branch 3 times, most recently from 8670aac to 962e682 Compare December 5, 2021 22:00
.split_whitespace()
.collect::<Vec<&str>>(),
);
for i in 0..quote_pos.len() / 2 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to me that you're trying to simulate .chunks(2) (and I can't wait for array_chunks(2) to be stabilized).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might also be easier to scan over the string and use a state machine, that way we can also handle escaping and also handle single quotes: https://github.com/tmiasko/shell-words/blob/8f264f1f9ec463a523c751f7bb56a07371db2b53/src/lib.rs#L108-L211

I'd use a Cow<'_, str> so that we can avoid allocating parts without escapes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's OK to do this in a follow-up though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the info on chunks, i rewrote that part and i think the code is now a lot cleaner and easier to understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it like this, because this way i can just use the str slices, no need to do extra allocations.
But in case it will be needed i can try to rewrite this using state machine.

Copy link
Member

@archseer archseer Dec 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to allocate if you use a state machine and only track offsets:

" => start = pos
.... pos++
" => start.is_some() -> end = pos
words.push(&input[start..end])

We only need to allocate if there's an escape character present. That's why I was suggesting Cow<'_, str>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, i understand it now, thanks for the explanation!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the changes, and it now supports quotes, doublequotes, escaping, also open and hsplit/vsplit can now open multiple files at once.

@ath3 ath3 force-pushed the open-space-filename branch 2 times, most recently from 3dc5520 to ee6f014 Compare December 9, 2021 18:37
cx.editor.set_error(format!("{}", e));
}
return;
}

// Handle typable commands
if let Some(cmd) = cmd::TYPABLE_COMMAND_MAP.get(parts[0]) {
if let Err(e) = (cmd.fun)(cx, &parts[1..], event) {
enum State {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move the parser into a new module in helix-core (shellwords maybe?) and add some unit tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, i pushed the changes.

Copy link
Member

@archseer archseer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants